import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Logger } from './../service/log4js';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const req = context.getArgByIndex(1).req;
    return next.handle().pipe(
      map((data) => {
        const logFormat = ` output
        url: ${req.originalUrl} -  ${req.method}
        IP: ${req.ip}
        Response data:  ${JSON.stringify(data)} `;
        Logger.info(logFormat);
        return data;
      }),
    );
  }
}
